package config

import (
	"gateway-server/internal/logger"
	"github.com/spf13/viper"
)

type AppConfig struct {
	Server struct {
		AppName  string `mapstructure:"app_name"`
		HttpPort int    `mapstructure:"http_port"`
		RunMode  string `mapstructure:"run_mode"`
	} `mapstructure:"server"`
	Etcd struct {
		Endpoints []string `mapstructure:"endpoints"`
	} `mapstructure:"etcd"`
}

func LoadAppConfig(configFile string) *AppConfig {
	return Load[AppConfig](configFile)
}
func Load[c any](configFile string) *c {
	var cfg c
	viper.SetConfigFile(configFile)
	if err := viper.ReadInConfig(); err != nil {
		panic(err)
	}
	if err := viper.Unmarshal(&cfg); err != nil {
		logger.Default.Errorf("config: unmarshal config failed")
	}
	return &cfg
}
